package engine.graphics.vfx
{
	import away3d.core.managers.*;
	import engine.asset.*;
	import engine.core.*;
	import engine.graphics.anim.*;
	import engine.graphics.away3d.*;
	import engine.graphics.lighting.*;
	import engine.graphics.particles.*;
	import engine.graphics.resources.*;
	import engine.graphics.sprite.*;
	import engine.loaders.*;
	import engine.math.*;
	import flash.display3D.*;
	import game.graphics.lighting.*;
	import game.graphics.shader.*;
	
	public class RxWorldFXManager
	{
		private var m_spriteResources:RxSprite3DResources;
		private var m_quadResources:RxQuadMeshResources;
		private var m_stage3DProxy:Stage3DProxy;
		private var m_shaderFamilyMeshFX:RxShaderFamilyMeshFX;
		private var m_shaderFamilyParticle:RxShaderFamilyParticle;
		private var m_pointLightMesh:RxPointLightMesh;
		private var m_spriteFXArray:Object;
		private var m_quadFXArray:Object;
		private var m_meshFXArray:Object;
		private var m_particleFXArray:Object;
		private var m_lightFXArray:Object;
		private var m_opaqueWorldFXArray:Vector.<RxWorldBaseFX>;
		private var m_translucentWorldFXArray:Vector.<RxWorldBaseFX>;
		private var m_refractingLayersWorldFXArray:Vector.<Vector.<RxWorldBaseFX>>;
		private var m_worldLightsArray:Vector.<RxWorldBaseFX>;
		private var m_visibleOpaqueWorldFXArray:Vector.<RxWorldBaseFX>;
		private var m_visibleTranslucentWorldFXArray:Vector.<RxWorldBaseFX>;
		private var m_visibleRefractingLayersWorldFXArray:Vector.<Vector.<RxWorldBaseFX>>;
		private var m_visibleWorldLightsArray:Vector.<RxWorldBaseFX>;
		private var m_camera:RxCamera3D;
		private var m_chains:Object;
		private var m_enabled:Boolean;
		private const m_usePool:Boolean = true;
		private var m_ViewDistCompareFunction:Function;
		private static var s_instance:RxWorldFXManager = null;
		
		public function RxWorldFXManager(param1:Stage3DProxy, param2:RxCamera3D)
		{
			var _loc_4:uint = 0;
			this.m_ViewDistCompareFunction = this.ViewDistCompareFunction;
			s_instance = this;
			this.m_stage3DProxy = param1;
			this.m_camera = param2;
			var _loc_3:* = param1.context3D;
			this.m_spriteResources = new RxSprite3DResources(_loc_3);
			this.m_quadResources = new RxQuadMeshResources(_loc_3);
			this.m_shaderFamilyMeshFX = null;
			this.m_shaderFamilyParticle = null;
			this.m_pointLightMesh = new RxPointLightMesh();
			this.m_pointLightMesh.CreateMesh(_loc_3, 8);
			this.m_spriteFXArray = new Object();
			this.m_quadFXArray = new Object();
			this.m_meshFXArray = new Object();
			this.m_particleFXArray = new Object();
			this.m_lightFXArray = new Object();
			this.m_opaqueWorldFXArray = new Vector.<RxWorldBaseFX>;
			this.m_translucentWorldFXArray = new Vector.<RxWorldBaseFX>;
			this.m_refractingLayersWorldFXArray = new Vector.<Vector.<RxWorldBaseFX>>(2, true);
			_loc_4 = 0;
			while (_loc_4 < this.m_refractingLayersWorldFXArray.length)
			{
				
				this.m_refractingLayersWorldFXArray[_loc_4] = new Vector.<RxWorldBaseFX>;
				_loc_4 = _loc_4 + 1;
			}
			this.m_worldLightsArray = new Vector.<RxWorldBaseFX>;
			this.m_visibleOpaqueWorldFXArray = new Vector.<RxWorldBaseFX>;
			this.m_visibleTranslucentWorldFXArray = new Vector.<RxWorldBaseFX>;
			this.m_visibleRefractingLayersWorldFXArray = new Vector.<Vector.<RxWorldBaseFX>>(2, true);
			_loc_4 = 0;
			while (_loc_4 < this.m_visibleRefractingLayersWorldFXArray.length)
			{
				
				this.m_visibleRefractingLayersWorldFXArray[_loc_4] = new Vector.<RxWorldBaseFX>;
				_loc_4 = _loc_4 + 1;
			}
			this.m_visibleWorldLightsArray = new Vector.<RxWorldBaseFX>;
			this.m_chains = new Object();
			this.m_enabled = true;
		}
		
		private function ViewDistCompareFunction(param1:RxWorldBaseFX, param2:RxWorldBaseFX) : Number
		{
			var _loc_3:* = param1.GetAccumWorldMatrix();
			var _loc_4:* = param2.GetAccumWorldMatrix();
			var _loc_5:* = _loc_3.m30;
			var _loc_6:* = _loc_3.m31;
			var _loc_7:* = _loc_3.m32;
			var _loc_8:* = _loc_4.m30;
			var _loc_9:* = _loc_4.m31;
			var _loc_10:* = _loc_4.m32;
			var _loc_11:* = this.m_camera.GetFrustumPlanes()[4];
			var _loc_12:* = this.m_camera.GetFrustumPlanes()[4].v.x * _loc_5 + _loc_11.v.y * _loc_6 + _loc_11.v.z * _loc_7 + _loc_11.v.w;
			var _loc_13:* = _loc_11.v.x * _loc_8 + _loc_11.v.y * _loc_9 + _loc_11.v.z * _loc_10 + _loc_11.v.w;
			_loc_12 = _loc_12 - param1.GetDepthBias();
			_loc_13 = _loc_13 - param2.GetDepthBias();
			return _loc_13 - _loc_12;
		}
		
		public function Spawn(param1:RxLoader3D, param2:RxWorldFXParams) : RxWorldBaseFX
		{
			
			var _loc_8:RxAnimTextureSheet = null;
			var _loc_9:RxWorldSpriteFX = null;
			var _loc_10:RxWorldQuadMeshFX = null;
			var _loc_11:RxWorldMeshFX = null;
			var _loc_12:RxWorldParticleFX = null;
			var _loc_13:RxParticleEmitter = null;
			var _loc_14:RxWorldLightFX = null;
			var _loc_15:uint = 0;/*
			if (!this.m_enabled)
			{
				return null;
			}
			var _loc_3:RxWorldBaseFX = null;
			var _loc_4:RxAnimSprite3D = null;
			var _loc_5:RxFrameAnimation = null;
			var _loc_6:RxFrameAnimation = null;
			var _loc_7:Boolean = false;
			if (param2.GetType() == RxWorldFXParams.SPRITE)
			{
				_loc_3 = this.m_spriteFXArray[param2.GetName()];
				if (_loc_3 != null)
				{
					_loc_3 = _loc_3.AllocInstance(this.m_usePool, param2);
					_loc_9 = RxWorldSpriteFX(_loc_3);
					_loc_4 = RxAnimSprite3D(_loc_9.GetSprite());
					_loc_4.SetWidth(param2.GetQuadWidth());
					_loc_4.SetHeight(param2.GetQuadHeight());
					_loc_5 = _loc_4.GetAnimTextureSheet().GetFrameAnimation();
					_loc_8 = _loc_9.GetRefractionAnimTexture();
					if (_loc_8 != null)
					{
						_loc_6 = _loc_8.GetFrameAnimation();
					}
					_loc_7 = _loc_4.IsBlendEnabled();
				}
			}
			else if (param2.GetType() == RxWorldFXParams.QUAD)
			{
				_loc_3 = this.m_quadFXArray[param2.GetName()];
				if (_loc_3 != null)
				{
					_loc_3 = _loc_3.AllocInstance(this.m_usePool, param2);
					_loc_10 = RxWorldQuadMeshFX(_loc_3);
					_loc_8 = _loc_10.GetAnimTexture();
					if (_loc_8 != null)
					{
						_loc_5 = _loc_8.GetFrameAnimation();
					}
					_loc_8 = _loc_10.GetRefractionAnimTexture();
					if (_loc_8 != null)
					{
						_loc_6 = _loc_8.GetFrameAnimation();
					}
					_loc_7 = _loc_10.IsBlendEnabled();
				}
			}
			else if (param2.GetType() == RxWorldFXParams.MESH)
			{
				_loc_3 = this.m_meshFXArray[param2.GetName()];
				if (_loc_3 != null)
				{
					_loc_3 = _loc_3.AllocInstance(this.m_usePool, param2);
					_loc_11 = RxWorldMeshFX(_loc_3);
					_loc_8 = _loc_11.GetAnimTexture();
					if (_loc_8 != null)
					{
						_loc_5 = _loc_8.GetFrameAnimation();
					}
					_loc_8 = _loc_11.GetRefractionAnimTexture();
					if (_loc_8 != null)
					{
						_loc_6 = _loc_8.GetFrameAnimation();
					}
					_loc_7 = _loc_11.IsBlendEnabled();
				}
			}
			else if (param2.GetType() == RxWorldFXParams.PARTICLES)
			{
				_loc_3 = this.m_particleFXArray[param2.GetName()];
				if (_loc_3 != null)
				{
					_loc_3 = _loc_3.AllocInstance(this.m_usePool, param2);
					_loc_12 = RxWorldParticleFX(_loc_3);
					_loc_13 = _loc_12.GetParticleEmitter();
					_loc_7 = _loc_13.IsBlendEnabled();
				}
			}
			else if (param2.GetType() == RxWorldFXParams.POINT_LIGHT)
			{
				_loc_3 = this.m_lightFXArray[param2.GetName()];
				if (_loc_3 != null)
				{
					_loc_3 = _loc_3.AllocInstance(this.m_usePool, param2);
					_loc_14 = RxWorldLightFX(_loc_3);
				}
			}
			if (_loc_3 != null)
			{
				if (_loc_5 != null)
				{
					_loc_5.Restart();
				}
				if (_loc_6 != null)
				{
					_loc_6.Restart();
				}
				_loc_3.Spawn(param2, param1);
				if (param2.GetType() == RxWorldFXParams.POINT_LIGHT)
				{
					this.m_worldLightsArray.push(_loc_3);
				}
				else if (_loc_3.GetRefractionTexture() != null || _loc_3.GetRefractionAnimTexture() != null)
				{
					_loc_15 = param2.GetLayer();
					if (_loc_15 < this.m_refractingLayersWorldFXArray.length)
					{
						this.m_refractingLayersWorldFXArray[_loc_15].push(_loc_3);
					}
				}
				else if (_loc_7)
				{
					this.m_translucentWorldFXArray.push(_loc_3);
				}
				else
				{
					this.m_opaqueWorldFXArray.push(_loc_3);
				}
			}
			return _loc_3;*/
			
			return null;
		}
		
		public function ParseFromXML(param1:XML, param2:RxAssetGroup) : void
		{
			
		}
		
		public function RegisterChain(param1:String, param2:RxWorldFXChain) : void
		{
			this.m_chains[param1] = param2;
		}
		
		public function Enable(param1:Boolean) : void
		{
			this.m_enabled = param1;
		}
		
		public static function get instance() : RxWorldFXManager
		{
			return s_instance;
		}
	}
}